"""
Case Type   : cm
Case Name   : delay_arbitrate_timeout参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、修改设置delay_arbitrate_timeout参数值为2
    3、修改设置delay_arbitrate_timeout参数值为30
    4、修改设置delay_arbitrate_timeout参数值为小数30.10
    5、修改设置delay_arbitrate_timeout参数值为-1，小于下限值
    6、修改设置delay_arbitrate_timeout参数值为2147483648，大于上限值
    7、修改设置delay_arbitrate_timeout参数值为0，下限值
    8、修改设置delay_arbitrate_timeout参数值为2147483647，上限值
    9、恢复参数默认值
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、设置成功
    3、设置成功
    4、设置失败
    5、设置失败
    6、设置失败
    7、设置成功
    8、设置成功
    9、恢复成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'delay_arbitrate_timeout'
        res = self.comsh.exec_cm_ctl('list', '--param --server',
                                     f'grep | {self.param}')
        self.param_default = res.splitlines()[0].split('=')[1].strip()
        self.log.info(self.param_default)

    def test_cm(self):
        text = '----step1:查看集群状态，查询AZ分布; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv', '-z ALL')
        self.log.info(status)
        AZ_status = status.splitlines()[-1]
        self.log.info(AZ_status)
        self.assertTrue('AZ1' in status and 'AZ2' in status, '集群部署方式不对')

        text = '----step2:修改设置delay_arbitrate_timeout参数值为2; expect:设置成功----'
        self.log.info(text)

        set1 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=2')
        self.log.info(set1)
        self.assertIn(self.constant.cm_set_param_success_msg, set1, '设置失败')

        text = '------step3:修改设置delay_arbitrate_timeout参数值为30; expect:设置成功------'
        self.log.info(text)

        set2 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=30')
        self.log.info(set2)
        self.assertIn(self.constant.cm_set_param_success_msg, set2, '设置失败')

        text = '------step4:修改设置delay_arbitrate_timeout参数值为小数30.10; expect:设置失败------'
        self.log.info(text)

        set3 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=30.10')
        self.log.info(set3)
        self.assertIn('set cm_server.conf fail', set3, '设置成功')

        text = '------step5:修改设置delay_arbitrate_timeout参数值为-1，小于下限值; expect:设置失败------'
        self.log.info(text)

        set4 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=-1')
        self.log.info(set4)
        self.assertIn('set cm_server.conf fail', set4, '设置成功')

        text = '------step6:修改设置delay_arbitrate_timeout参数值为2147483648，大于上限值; expect:设置失败------'
        self.log.info(text)

        set5 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=2147483648')
        self.log.info(set5)
        self.assertIn('set cm_server.conf fail', set5, '设置成功')

        text = '------step7:修改设置delay_arbitrate_timeout参数值为0，下限值; expect:设置成功------'
        self.log.info(text)

        set6 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=0')
        self.log.info(set6)
        self.assertIn(self.constant.cm_set_param_success_msg, set6, '设置成功')

        text = '------step8:修改设置delay_arbitrate_timeout参数值为2147483647，上限值; expect:设置成功------'
        self.log.info(text)

        set7 = self.comsh.exec_cm_ctl('set', '--param --server',
                                      f'-k {self.param}=2147483647')
        self.log.info(set7)
        self.assertIn(self.constant.cm_set_param_success_msg, set7, '设置成功')

    def tearDown(self):
        text = '------step9:恢复参数默认值; expect:恢复成功------'
        self.log.info(text)

        recovery = self.comsh.exec_cm_ctl('set', '--param --server',
                                          f'-k {self.param}={self.param_default}')
        self.log.info(recovery)
        reload = self.comsh.exec_cm_ctl('reload', '--param --server')
        self.log.info(reload)
        self.assertIn(self.constant.cm_set_param_success_msg, recovery, '参数设置失败')
        self.assertIn(self.constant.cm_reload_success_msg, reload, '参数加载失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
